package com.qf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dto.ApiResult;
import com.qf.entity.Address;
import com.qf.service.IAddressService;
import com.qf.utils.R;
import com.qf.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址相关接口")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @PostMapping("/list")
    @ApiOperation("用户地址列表")
    @CrossOrigin
    public ApiResult list(){
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", UserUtils.getUser().get("id"));
        queryWrapper.orderByAsc("is_default");
        List<Address> list = addressService.list(queryWrapper);
        return R.ok(list);
    }

    @PostMapping("/changeDefault/{id}")
    @ApiOperation("改变用户默认地址")
    @CrossOrigin
    public ApiResult changeDefault(@PathVariable String id){
        addressService.changeDefault(id);
        return R.ok();
    }
    @PostMapping("/add")
    @ApiOperation("添加用户地址")
    @CrossOrigin
    public ApiResult add(@RequestBody Address address){

        addressService.add(address);
        return R.ok();
    }


}
